package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.MyPage;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_advpos")
public class YddAdvposController {

    @Autowired
    private IYddAdvposService iYddAdvposService;

    @Autowired
    private IYddAdvimgService iYddAdvimgService;

    /**
     * 广告位列表分页
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ResultData find(@RequestParam Map<String, String> map){
        //当前页码
        int page = Integer.parseInt(map.get("page"));
        //每页数量
        int pageSize = Integer.parseInt(map.get("limit"));
        Page<YddAdvpos> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询
        queryWrapper.like(StringUtils.isNotBlank(map.get("advposdesc")),YddAdvpos::getAdvposdesc,map.get("advposdesc"));
        queryWrapper.like(StringUtils.isNotBlank(map.get("advposname")),YddAdvpos::getAdvposname,map.get("advposname"));
        iYddAdvposService.page(pageInfo,queryWrapper);
        //数据
        List<YddAdvpos> records = pageInfo.getRecords();
        //总条数
        long count = records.size();
        //总页数
        long totalPage = count % pageSize == 0 ? count/pageSize : count/pageSize + 1;

        List<YddAdvposDto> dtoList = new ArrayList<>();

        //广告位数量
        for (YddAdvpos record : records) {
            YddAdvposDto yddAdvposDto = new YddAdvposDto();
            BeanUtils.copyProperties(record,yddAdvposDto);
            LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<>();
            lqw.eq(YddAdvimg::getAdvimgpos,record.getId());
            List<YddAdvimg> list = iYddAdvimgService.list(lqw);
            yddAdvposDto.setAdvposnum(list.size());
            dtoList.add(yddAdvposDto);
        }
        MyPage myPage = new MyPage(count,totalPage,(long)pageSize,(long) page,dtoList);
        return ResultData.ok("",myPage);
    }

    /**
     * 广告位修改
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvpos yddAdvpos){
        iYddAdvposService.updateById(yddAdvpos);
        return ResultData.ok("","");
    }

    /**
     * 广告位删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(long id){
        iYddAdvposService.removeById(id);
        return ResultData.ok("","");
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/all")
    public ResultData findAll(){
        List<YddAdvpos> list = iYddAdvposService.list();
        return ResultData.ok("",list);
    }

    /**
     * 查询单个 广告位详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData findById(long id){
        YddAdvpos advpos = iYddAdvposService.getById(id);
        return ResultData.ok("",advpos);
    }

    /**
     * 广告位添加
     * @param yddAdvpos
     * @return
     */
    @PostMapping("add")
    public ResultData save(@RequestBody YddAdvpos yddAdvpos){
        iYddAdvposService.save(yddAdvpos);
        return ResultData.ok("","");
    }
}